<?php
//
// Survey meta functions
//


function get_survey_meta($survey_id, $meta_key = '') {
	global $wpdb, $wp_surveys_tables;
	
	$survey_id = (int) $survey_id;

	if ( !$survey_id )
		return false;

	if ( !empty($meta_key) ) {
		$meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
		
		// TODO need to look into using this.
		//$user = wp_cache_get($user_id, 'users');
		
		// Check the cached user object
		//if ( false !== $user && isset($user->$meta_key) )
		//	$metas = array($user->$meta_key);
		//else
		$metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $wp_surveys_tables['survey_meta'] . " WHERE survey_id = %d AND meta_key = %s", $survey_id, $meta_key) );
	} else {
		$metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $wp_surveys_tables['survey_meta'] . " WHERE survey_id = %d", $survey_id) );
	}

	if ( empty($metas) ) {
		if ( empty($meta_key) )
			return array();
		else
			return '';
	}

	$metas = array_map('maybe_unserialize', $metas);

	if ( count($metas) == 1 )
		return $metas[0];
	else
		return $metas;
}	

function set_survey_meta( $survey_id, $meta_key, $meta_value ) {
	global $wpdb, $wp_surveys_tables;
	
	if ( !is_numeric( $survey_id ) )
		return false;
	
	$meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
	$meta_value = (string)$meta_value;

	if ( is_string($meta_value) )
		$meta_value = stripslashes($wpdb->escape($meta_value));
	
	$meta_value = maybe_serialize($meta_value);
	
	if (empty($meta_value)) {
		return delete_survey_meta( $survey_id, $meta_key );
	}

	$cur = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wp_surveys_tables['survey_meta'] . " WHERE survey_id = %d AND meta_key = %s", $survey_id, $meta_key ) );

	if ( !$cur ) {
		$wpdb->query( $wpdb->prepare( "INSERT INTO " . $wp_surveys_tables['survey_meta'] . " ( survey_id, meta_key, meta_value ) VALUES ( %d, %s, %s )", $survey_id, $meta_key, $meta_value ) );
	} else if ( $cur->meta_value != $meta_value ) {
		$wpdb->query( $wpdb->prepare( "UPDATE " . $wp_surveys_tables['survey_meta'] . " SET meta_value = %s WHERE survey_id = %d AND meta_key = %s", $meta_value, $survey_id, $meta_key ) );
	} else {
		return false;
	}

	// TODO need to look into using this.
	// wp_cache_delete($user_id, 'users');

	return true;
}

function delete_survey_meta( $survey_id, $meta_key = false, $meta_value = false ) {
	global $wpdb, $bp;
	
	if ( !is_numeric( $survey_id ) )
		return false;
		
	$meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);

	if ( is_array($meta_value) || is_object($meta_value) )
		$meta_value = serialize($meta_value);
		
	$meta_value = trim( $meta_value );

	if ( !$meta_key ) {
		$wpdb->query( $wpdb->prepare( "DELETE FROM " . $wp_surveys_tables['survey_meta'] . " WHERE survey_id = %d", $survey_id ) );		
	} else if ( !$meta_value ) {
		$wpdb->query( $wpdb->prepare( "DELETE FROM " . $wp_surveys_tables['survey_meta'] . " WHERE survey_id = %d AND meta_key = %s AND meta_value = %s", $survey_id, $meta_key, $meta_value ) );
	} else {
		$wpdb->query( $wpdb->prepare( "DELETE FROM " . $wp_surveys_tables['survey_meta'] . " WHERE survey_id = %d AND meta_key = %s", $survey_id, $meta_key ) );
	}
	
	// TODO need to look into using this.
	// wp_cache_delete($survey_id, 'surveys');

	return true;
}

?>